/*
* CSET 3600
* Spring Semester 2013
* Melissa Brown
* Josh Siebenaler
* Matt Rich
* Christian Angelo
*
*/
package mancala;


public class HardAI {
    
    public static int ChooseHole;
    public static String ChooseHoleTrigger;
    public static boolean Capture;
    private static int [] tempvalue = new int [16];
    public static int [] acrossvalue = new int[8];
    public static int [] totalvalues = new int [8];
    public static int [] legalcapturevalues = new int [10];
    public static int [] holechoices = new int [8];
    public static int [] newholechoices = new int[8];
    public static int legalcapturevaluescounter = 1;
    
    
    
    public static int Options(int [] ReturnHoleValue){
        
        for (int i = 0; i < tempvalue.length; i ++){
            
            tempvalue[i] = ReturnHoleValue[i];
            System.out.print(tempvalue[i]);
            
        }
        
        
               
        
        ChooseHole = FirstMove(ChooseHole);//check this first as it will
       
               
        if(ChooseHole == -1){
                   
            ChooseHole = CanWeCapture(tempvalue,ChooseHole);
                   
                   System.out.println("new choosehole is" + ChooseHole);
          } 
               
        if(ChooseHole == -2){
                   
             ChooseHole = ChooseRandomHole.randomvalue();
             
          }

     System.out.println("ChooseHole is " + ChooseHole);
     
        
        return ChooseHole;
    
    
}
    
    public static int FirstMove(int ChooseHole){
        
        if(ChooseHole > 1){
            
            ChooseHole = 0;
        }
        
        if(ChooseHole < -1){
            
            ChooseHole = 0;
        }
        
        if(tempvalue[1] == 7){
            
            ChooseHole = 1;
        }
        else{
            ChooseHole = -1;
        }
        
        
        
        return ChooseHole;
       
    }
    
    public static int CanWeCapture(int [] tempvalue, int ChooseHole){
        
        
        int potentialcapturehole = 0;
        int potentialcounter = 0;
        int captureposition = 0;
        int [] potentialcapturevalues = new int [8];
        int [] potentialcapturefinalhole = new int[8];
        int lasthole = 0;
        potentialcapturehole = 0;
        int capturecounter = 1;
        
        for(int i = 1; i <=7; i ++){
            
            
            if(tempvalue[i] < 7 && tempvalue[i] > 0){
              
                
                potentialcapturevalues[captureposition] = i;
                System.out.println("hole at " + i);
                holechoices[capturecounter] = i;
                System.out.println("hole at  "+ capturecounter + " is " + holechoices[capturecounter]);
                capturecounter +=1;
                
                potentialcapturefinalhole[captureposition ] = i + tempvalue[i];
                captureposition += 1;
                potentialcapturehole += 1;
            }
    }
        System.out.println("p" + potentialcapturehole);
          
        
        if(potentialcapturehole == 0){
                
                ChooseHole = -2;
            }else{
                
                RemoveOutofBoundsHoles(potentialcapturefinalhole, potentialcapturevalues);
                
                
            }
        
        for(int i = 0; i < potentialcapturevalues.length; i ++){
            
            if(potentialcapturevalues[i] > 0){
                
                 legalcapturevalues[legalcapturevaluescounter]= potentialcapturefinalhole[i]; 
                System.out.println("l " +legalcapturevalues[legalcapturevaluescounter]);
                legalcapturevaluescounter +=1;
                
                
                
            }
            else{
                
                potentialcounter += 1;
            }
        }
        
        System.out.println(potentialcounter);
        
        
        if(potentialcounter == 8){
            
            ChooseHole = -2;
            
        } else{
            
            legalcapturevaluescounter = 1;
            totalvalues = ComputeCaptureTotals(legalcapturevalues,tempvalue);
            
         }
        
        System.out.println("totalvalue " +totalvalues[1]);//problem here when
        //return totalvalues from computecapturetotl
        
        
        if(totalvalues[1] == 0){
            System.out.println("bad values " + totalvalues[0]);
            
            ChooseHole = -2;
        }
        else{
            
            int holechoice = FindBiggestTotal(totalvalues);
            System.out.println("holechoice" + holechoice);
            ChooseHole = holechoices[holechoice];
            
            System.out.println("choosehole " + ChooseHole);
            
        }
        
        
        return ChooseHole;
    }
    
    public static int RemoveOutofBoundsHoles(int [] finalhole, int [] holevalue){
        for(int i = 0; i < holevalue.length; i ++){
            
            if(finalhole[i] > 7){
                
                holevalue[i] = 0;
                finalhole[i] = 0;
               
                
            }
        }

        
        return 0;
    }
    
    public static int[] ComputeCaptureTotals(int [] legalcapture, int [] tempvalue){
        
        int acrossvalue [] = ComputeAcrossCaptureHoles();
        int [] total = new int [8];
        
        int choicecounter = 1;
        
        for(int i = 1; i < total.length; i ++){
            
            //if(tempvalue[i] == 0){
              //  i += 1;
           // }
            
            if(legalcapture[i] > 0){
                
                if(legalcapture[i] > 7){
                    
                    legalcapture[i] = 0;
                }
               
                System.out.println("legal " + legalcapture[i]);
                System.out.println("i is" + i);
               
                
            
            if(tempvalue[legalcapture[i]]== 0){
                total[choicecounter] = tempvalue[legalcapture[i]] + tempvalue[acrossvalue[legalcapture[i]]];//need value that is across
                //from the value at legalcapture
                System.out.println("Acc" + acrossvalue[legalcapture[i]]);
                System.out.println("value " + legalcapture[i] + " has " + total[choicecounter]);
                newholechoices[choicecounter] = i;
                System.out.println("legal 2values are " + newholechoices[choicecounter]);
                choicecounter +=1;
                 
                 
            }
            }
            
            
            
            
        }
        
        
        return total;
        
        
    }
    
    public static int[] ComputeAcrossCaptureHoles(){
        int [] acrosshole = new int [8];
        int acrossvalue = 15;
        
     for(int i = 1; i < acrosshole.length; i ++){
         
         acrosshole[i] = acrossvalue;
        
         acrossvalue -= 1;
         
         if(acrossvalue > 15){
             acrossvalue = 15;
         }
         
         if(acrossvalue < 9){
             acrossvalue = 9;
         }
         
     }

        return acrosshole;
     
    }
    
    
    public static int FindBiggestTotal(int [] total){
        int HolePosition = 0;
        int HoleCounter;
        
            if(total[1] < total[0]){
                
                HolePosition = 1;
                System.out.println("choosehole at 1" + HolePosition);
            }
        
        for(int i = 1; i < total.length; i ++){
            
          
          
            
            if(total[i] > total[HolePosition] ){
                
                HolePosition  =  newholechoices[i];
                System.out.println("choosehole" + HolePosition);
            } 
        }
        
        
        
        return HolePosition;
        
    }
    
}
